import requests
from datetime import datetime
import time

def get_gitee_repo_creation_time(owner, repo, access_token=None):
    """
    查询Gitee仓库的创建时间
    
    参数:
        owner: 仓库所有者用户名
        repo: 仓库名称
        access_token: 可选，Gitee个人访问令牌(用于私有仓库或提高API限制)
    
    返回:
        仓库创建时间的datetime对象
    """
    # Gitee API端点
    url = f"https://gitee.com/api/v5/repos/{owner}/{repo}"
    
    # 请求参数
    params = {}
    if access_token:
        params['access_token'] = access_token
    
    try:
        # 发送GET请求
        response = requests.get(url, params=params)
        response.raise_for_status()  # 检查请求是否成功
        
        # 解析JSON响应
        repo_info = response.json()
        
        # 提取并转换创建时间
        created_at = repo_info['created_at']
        creation_time = datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%S%z")
        
        return creation_time
    
    except requests.exceptions.RequestException as e:
        print(f"请求Gitee API时出错: {e}")
        return None
    except KeyError:
        print("无法从响应中解析创建时间")
        return None
    except ValueError:
        print("时间格式解析错误")
        return None

def run(repo):
    
    # 如果有私有仓库或需要提高API限制，可以添加access_token
    # access_token = "your_gitee_access_token"
    access_token = None
    creation_time = get_gitee_repo_creation_time(owner, repo, access_token)
    if creation_time:
        print(f"{owner}/{repo}  {creation_time}")
    else:
        print(f"{owner}/{repo}  无法获取仓库创建时间")

def read_repo_list(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        repo_list = [line.strip() for line in file if line.strip()]
    return repo_list

# 使用示例
if __name__ == "__main__":
    # 替换为你要查询的仓库信息
    owner = "openharmony"
    input_file = "135.txt"
    repo_list = read_repo_list(input_file)
    for i in repo_list:
        run(i)
        time.sleep(0.5)
